package testy;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginPanel extends JPanel {
	
	private JFrame myFrame;
	private Data db;
	
	public JTextField getPinField() {
		return pinField;
	}

	public void setPinField(JTextField pinField) {
		this.pinField = pinField;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	private JTextField pinField;
	private JPasswordField passwordField;
	private JButton loginButton;
	
	
	public LoginPanel(final JFrame theFrame, final Data theDB) {
		super();
		myFrame = theFrame;
		db = theDB;
		pinField = new JTextField("PIN");
		passwordField = new JPasswordField("password");
		loginButton = new JButton("Login");
		setup();
		
	}
	
	private void setup() {
		setLayout(null);
		pinField.setBounds(140, 154, 155, 28);
		pinField.setColumns(10);
		passwordField.setBounds(140, 186, 155, 20);
		
		add(pinField);
		add(passwordField);
        loginButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent theEvent) {
                       
                        final String pinNumber = pinField.getText();
                        final String password = passwordField.getText();
                        //Verify to database.
                        //try {
                        	int pin = Integer.parseInt(pinNumber);
                        	User temp = db.tryLogin(pin, password);
                            
                            if (temp != null && !temp.isAdmin()) {
                                    CardLayout c = ((CardLayout) (myFrame.getContentPane()).getLayout());
                                    c.show(myFrame.getContentPane(), "AnalystPanel");
                            } else if (temp != null && temp.isAdmin()) {
                                    CardLayout c = ((CardLayout) (myFrame.getContentPane()).getLayout());
                                    c.show(myFrame.getContentPane(), "AdminPanel");
                            }
                        //} catch (Exception e) {/* Do nothing */}
                }
        });
        loginButton.setBounds(161, 218, 117, 29);
        add(loginButton);
        setVisible(true);
	}
}
